#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
//    ui->setupUi(this);
//    imgInit();

    test();
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::imgInit()
{
    QString imgPath = "test.jpg";
    Mat imgData = imread(imgPath.toLatin1().data());
    if(imgData.empty()){qDebug("no img");return;}
    cvtColor(imgData,imgData,COLOR_BGR2RGB);
    myImg = imgData;

    myBlurQImg = QImage((const unsigned char*)(imgData.data),imgData.cols,imgData.rows,QImage::Format_RGB888);
    myGaussianQImg   =   myBlurQImg;
    myMedianQImg    =   myBlurQImg;
    myBilateralQImg =   myBlurQImg;

    imgShow();
}

void MainWindow::imgShow()
{
    ui->labAverage->setPixmap(QPixmap::fromImage(myBlurQImg.scaled(ui->labAverage->size(),Qt::KeepAspectRatio)));
    ui->labGaussian->setPixmap(QPixmap::fromImage(myGaussianQImg.scaled(ui->labGaussian->size(),Qt::KeepAspectRatio)));
    ui->labMedian->setPixmap(QPixmap::fromImage(myMedianQImg.scaled(ui->labMedian->size(),Qt::KeepAspectRatio)));
    ui->labBilateral->setPixmap(QPixmap::fromImage(myBilateralQImg.scaled(ui->labBilateral->size(),Qt::KeepAspectRatio)));
}

void MainWindow::imgProc(int kernel)
{
    Mat src = myImg;

    Mat dst1 = src.clone();
    for (int i=1 ; i<kernel ; i=i+2 ) cv::blur(src,dst1,Size(i,i),Point(-1,-1));
    myBlurQImg = QImage((const unsigned char*)(dst1.data),dst1.cols,dst1.rows,QImage::Format_RGB888);

    Mat dst2 = src.clone();
    for (int i=1 ; i<kernel ; i=i+2 ) cv::GaussianBlur(src,dst2,Size(i,i),0,0);
    myGaussianQImg = QImage((const unsigned char*)(dst2.data),dst2.cols,dst2.rows,QImage::Format_RGB888);

    Mat dst3 = src.clone();
    for (int i=1 ; i<kernel ; i=i+2 ) cv::medianBlur(src,dst3,i);
    myMedianQImg = QImage((const unsigned char*)(dst3.data),dst3.cols,dst3.rows,QImage::Format_RGB888);

    Mat dst4 = src.clone();
    for (int i=1 ; i<kernel ; i=i+2 ) cv::bilateralFilter(src,dst4,i,i*2,i/2);
    myBilateralQImg = QImage((const unsigned char*)(dst4.data),dst4.cols,dst4.rows,QImage::Format_RGB888);

    imgShow();
}

void MainWindow::on_verticalSlider_sliderMoved(int position)
{
    imgProc(position);
}

void MainWindow::on_verticalSlider_valueChanged(int value)
{
    imgProc(value);
}


void MainWindow::on_btnsave_clicked()
{
    QString savefile = QFileDialog::getSaveFileName(this,tr("save file"),QDir::currentPath(),tr("pic(*.jpg *.png *.jpeg *.bmp)"));
    if(savefile.isEmpty())return;
    QScreen *screen = QGuiApplication::primaryScreen();

    if(ui->rbaverage->isChecked()) screen->grabWindow(ui->labAverage->winId()).save(savefile);
    if(ui->rbgaussian->isChecked()) screen->grabWindow(ui->labGaussian->winId()).save(savefile);
    if(ui->rbmedian->isChecked()) screen->grabWindow(ui->labMedian->winId()).save(savefile);
    if(ui->rbbilateral->isChecked()) screen->grabWindow(ui->labBilateral->winId()).save(savefile);
}

void MainWindow::test()
{
    Point3i a(2,3,4),b(5,6,7);

    Point3i c = a.cross(b);
    std::cout<<c<<std::endl;

    int d = a.dot(b);
    std::cout<<d<<std::endl;

    Mat Amat;
    Amat = Mat::eye(5,5,CV_64F)*3;
    std::cout<<Amat<<std::endl;

}
